package ui;

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import ui.GameOptions;

import messaging.Notification;
import messaging.NotificationManager;


public class Welcome extends JPanel {
	
	private JButton newgame, quit;
	BoardUI boardUi; 
	private GameOptions options;

	private JLabel label_1;
	/**
	 * Create the panel.
	 */
	public Welcome(BoardUI a) {
		boardUi = a;
		int frameWidth = 800, frameHeight = 480;
		int buttonWidth = 350, buttonHeight = 75;

		this.setSize(792, 400);

		this.setLayout(null);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

		Point location = new Point((int)(screen.getWidth() - frameWidth) / 2,
				((int)(screen.getHeight() - frameHeight) / 2));


		
		// Setup and create all 3 buttons
		Font buttonFont = new Font("Times New Roman", Font.BOLD, 26);

		newgame = new JButton("New Game");
		newgame.setBounds(212, 232, buttonWidth, buttonHeight);
		newgame.setFont(buttonFont);
		newgame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	
                if (options == null)
                	options= new GameOptions(Welcome.this);
                else {
                	options.showFirstPagePanels();
                	options.setVisible(true);
                }
                hideMenu();
            }

        });

	
		quit = new JButton("Quit Game");
		quit.setBounds(212, 314, 350, 75);
		quit.setFont(buttonFont);
		
		quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

		// Add some Components to the panel
		add(newgame);
		add(quit);
		
		// Add the Panel to the boardUi
		BoardDisplay board = BoardDisplay.getInstance();
		board.addComponent(this, location, JLayeredPane.PALETTE_LAYER);
		

		// Set the visibility as true, thereby Boarding it
		setVisible(true);
		
		// Add "this" to necessary event observer lists
		NotificationManager.getInstance().addObserver(Notification.START_GAME,
				this, "hideMenu");
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Welcome.class.getResource("/images/playeryellow.jpg")));
		label.setBounds(0, 0, 400, 400);
		add(label);
		
		label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(Welcome.class.getResource("/images/playeryellow.jpg")));
		label_1.setBounds(398, 0, 400, 400);
		add(label_1);
	}
	
	// Can be used to remove all Buttons within the Welcome class
	public void hidePanels() {
		newgame.setVisible(false);
        quit.setVisible(false);
	}
	
	public void hideMenu() {
		setVisible(false);
	}
	
	public void showMenu() {
		setVisible(true);
	}
	
	public static void main(String[] args) {
		BoardUI b = new BoardUI();
		new Welcome(b);

	}
}